package com.kmxd.ams.app.search.command;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjUtil;
import com.kmxd.ams.client.jy.IStatisticsSvc;
import com.kmxd.ams.client.jy.dto.StatisticsAddDTO;
import com.kmxd.ams.client.jy.vo.StatisticsVO;
import com.kmxd.ams.client.search.dto.QuickSearchDTO;
import com.kmxd.ams.client.system.ISysUserSvc;
import com.kmxd.ams.client.system.vo.SysUserVO;
import com.kmxd.ams.client.use.vo.UseArchListVO;
import com.kmxd.ams.core.common.constant.ArchControlConst;
import com.kmxd.ams.core.util.DynamicTableNameUtil;
import com.kmxd.ams.infra.search.entity.DocumentEs;
import com.kmxd.ams.infra.use.entity.UseOrder;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 利用统计
 */
@Slf4j
@Component
@AllArgsConstructor
public class StatisticsCmd {

    private IStatisticsSvc statisticsService;
  private ISysUserSvc sysUserSvc;

    /**
     * 执行查询统计
     *
     * @param userId
     * @param req
     * @param data
     */
    public void runSearch(Long userId, QuickSearchDTO req, List<DocumentEs> data) {
        try {
            StatisticsVO entity = statisticsService.view(userId);
            if (ObjUtil.isEmpty(entity)) {
                entity = new StatisticsVO();
                entity.setUserId(userId);
      }
      SysUserVO sysUser = sysUserSvc.view(userId);
      entity.setApplyDate(new Date());
      // 次
      entity.setMainOrder(1L);
      // 人次
      entity.setPersonOrder(
          ObjUtil.isEmpty(sysUser.getVisitorNumber()) ? 1L : sysUser.getVisitorNumber());

            List<DocumentEs> docList = data.stream().filter(x -> !DynamicTableNameUtil.checkTableVol(x.getTableId())).collect(Collectors.toList());
            List<DocumentEs> volList = data.stream().filter(x -> DynamicTableNameUtil.checkTableVol(x.getTableId())).collect(Collectors.toList());

            //卷次
            entity.setVolOrder(entity.getVolOrder() + volList.size());
            //件次
            entity.setDocOrder(entity.getDocOrder() + docList.size());

            //档案载体(纸质)
            List<DocumentEs> carrierP = data.stream().filter(x -> ObjUtil.equal("纸质", x.getPhysicalCategory()) || ObjUtil.equal("1", x.getPhysicalCategory())).collect(Collectors.toList());
            entity.setCarrierP(entity.getCarrierP() + carrierP.size());

            //档案载体(数字化)
            List<DocumentEs> carrierD = data.stream().filter(x -> ObjUtil.equal("是", x.getDigital()) || ObjUtil.equal("1", x.getDigital())).collect(Collectors.toList());
            entity.setCarrierD(entity.getCarrierD() + carrierD.size());

            //档案载体(声像)
            List<DocumentEs> carrierS = data.stream().filter(x -> ObjUtil.notEqual("纸质", x.getPhysicalCategory()) && ObjUtil.notEqual("1", x.getPhysicalCategory())).collect(Collectors.toList());
            entity.setCarrierS(entity.getCarrierS() + carrierS.size());

            List<DocumentEs> kfq = data.stream().filter(x -> ObjUtil.notEqual(ArchControlConst.CTL_2, x.getControlIdentifier())).collect(Collectors.toList());
            List<DocumentEs> wkf = data.stream().filter(x -> !ObjUtil.notEqual(ArchControlConst.CTL_2, x.getControlIdentifier())).collect(Collectors.toList());

            entity.setKfq(entity.getKfq() + kfq.size());
            entity.setWkf(entity.getWkf() + wkf.size());

            StatisticsAddDTO statisticsAdd = BeanUtil.copyProperties(entity, StatisticsAddDTO.class);
            statisticsService.add(statisticsAdd);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("添加统计信息异常！{}", e.getMessage());
        }
    }

  /**
   * 申请统计
   *
   * @param userId
   * @param useOrder
   */
  public void runApply(Long userId, UseOrder useOrder, List<UseArchListVO> archList) {
    try {
      StatisticsVO entity = statisticsService.view(userId);
      if (ObjUtil.isEmpty(entity)) {
        entity = new StatisticsVO();
        entity.setUserId(userId);
      }
      entity.setApplyDate(new Date());
      // 次
      entity.setMainOrder(entity.getMainOrder() + 1);
      // 人次
      entity.setPersonOrder(entity.getPersonOrder() + 1);

      StatisticsAddDTO statisticsAdd = BeanUtil.copyProperties(entity, StatisticsAddDTO.class);
      statisticsService.add(statisticsAdd);
    } catch (Exception e) {
      e.printStackTrace();
      log.error("添加统计信息异常！{}", e.getMessage());
    }
  }
}
